package views;

import java.util.ArrayList;

import javaroadeditor.Activator;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import commands.AddBuilding;
import commands.AddGate;
import commands.AddQuaryCrane;
import commands.AddRailMountedGantry;
import commands.AddReachStacker;
import commands.AddRoadTruck;
import commands.AddRubberTiredGantry;
import commands.AddStack;
import commands.AddTerminal;
import commands.AddTerminalTruck;
import commands.AddVessel;

public class ComponentsTreeView extends ViewPart {
	public static final String ID = "JavaRoadEditor.componentsTreeView";
	private TreeViewer viewer;

	class TreeObject {
		private String name;
		private TreeParent parent;

		public TreeObject(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setParent(TreeParent parent) {
			this.parent = parent;
		}

		public TreeParent getParent() {
			return parent;
		}

		public String toString() {
			return getName();
		}
	}

	class TreeParent extends TreeObject {
		private ArrayList<TreeObject> children;

		public TreeParent(String name) {
			super(name);
			children = new ArrayList<TreeObject>();
		}

		public void addChild(TreeObject child) {
			children.add(child);
			child.setParent(this);
		}

		public void removeChild(TreeObject child) {
			children.remove(child);
			child.setParent(null);
		}

		public TreeObject[] getChildren() {
			return (TreeObject[]) children.toArray(new TreeObject[children.size()]);
		}

		public boolean hasChildren() {
			return children.size() > 0;
		}
	}

	class ViewContentProvider implements IStructuredContentProvider, ITreeContentProvider {

		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			return getChildren(parent);
		}

		public Object getParent(Object child) {
			if (child instanceof TreeObject) {
				return ((TreeObject) child).getParent();
			}
			return null;
		}

		public Object[] getChildren(Object parent) {
			if (parent instanceof TreeParent) {
				return ((TreeParent) parent).getChildren();
			}
			return new Object[0];
		}

		public boolean hasChildren(Object parent) {
			if (parent instanceof TreeParent)
				return ((TreeParent) parent).hasChildren();
			return false;
		}
	}

	class ViewLabelProvider extends LabelProvider {

		public String getText(Object obj) {
			return obj.toString();
		}

		public Image getImage(Object obj) {
			if (obj instanceof TreeParent) {
				String imageKey = ISharedImages.IMG_OBJ_FOLDER;
				return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
			} else if (((TreeObject) obj).getName().equals("Quary Crane")) {
				return Activator.getImageDescriptor("icons/QuaryCrane.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Building")) {
				return Activator.getImageDescriptor("icons/Building.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Rubber Tired Gantry")) {
				return Activator.getImageDescriptor("icons/RubberTiredGantry.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Rail Mounted Gantry")) {
				return Activator.getImageDescriptor("icons/RailMountedGantry.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Reach Stacker")) {
				return Activator.getImageDescriptor("icons/ReachStacker.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Road Truck")) {
				return Activator.getImageDescriptor("icons/RoadTruck.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Vessel")) {
				return Activator.getImageDescriptor("icons/Vessel.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Terminal Truck")) {
				return Activator.getImageDescriptor("icons/TerminalTruck.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Gate")) {
				return Activator.getImageDescriptor("icons/Gate.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Terminal")) {
				return Activator.getImageDescriptor("icons/Terminal.png").createImage();

			} else if (((TreeObject) obj).getName().equals("Stack")) {
				return Activator.getImageDescriptor("icons/Stack.png").createImage();

			} else {
				return Activator.getImageDescriptor("icons/alt_window_16.gif").createImage();
			}
		}
	}

	private TreeObject createDummyModel() {
		TreeObject to1 = new TreeObject("Quary Crane");
		TreeObject to2 = new TreeObject("Rail Mounted Gantry");
		TreeObject to3 = new TreeObject("Rubber Tired Gantry");
		TreeParent p1 = new TreeParent("Cranes");
		p1.addChild(to1);
		p1.addChild(to2);
		p1.addChild(to3);

		TreeObject to4 = new TreeObject("Reach Stacker");
		TreeObject to5 = new TreeObject("Road Truck");
		TreeObject to6 = new TreeObject("Vessel");
		TreeObject to7 = new TreeObject("Terminal Truck");
		TreeParent p2 = new TreeParent("Vehicles");
		p2.addChild(to4);
		p2.addChild(to5);
		p2.addChild(to6);
		p2.addChild(to7);

		TreeObject to8 = new TreeObject("Building");
		TreeObject to10 = new TreeObject("Terminal");
		TreeObject to11 = new TreeObject("Stack");
		TreeParent p3 = new TreeParent("Structures");
		p3.addChild(to8);
		p3.addChild(to10);
		p3.addChild(to11);

		TreeParent root = new TreeParent("");
		root.addChild(p1);
		root.addChild(p2);
		root.addChild(p3);
		return root;
	}

	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setInput(createDummyModel());
		viewer.expandAll();
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				if (event.getSelection().isEmpty()) {
					return;
				}
				if (event.getSelection() instanceof IStructuredSelection) {
					IStructuredSelection selection = (IStructuredSelection) event.getSelection();
					if (event.getSelection().toString().equals("[Quary Crane]")) {
						new AddQuaryCrane().execute(null);
					} else if (selection.toString().equals("[Building]")) {
						new AddBuilding().execute(null);
					} else if (selection.toString().equals("[Rubber Tired Gantry]")) {
						new AddRubberTiredGantry().execute(null);
					} else if (selection.toString().equals("[Gate]")) {
						new AddGate().execute(null);
					} else if (selection.toString().equals("[Terminal Truck]")) {
						new AddTerminalTruck().execute(null);
					} else if (selection.toString().equals("[Road Truck]")) {
						new AddRoadTruck().execute(null);
					} else if (selection.toString().equals("[Reach Stacker]")) {
						new AddReachStacker().execute(null);
					} else if (selection.toString().equals("[Terminal]")) {
						new AddTerminal().execute(null);
					} else if (selection.toString().equals("[Stack]")) {
						new AddStack().execute(null);
					} else if (selection.toString().equals("[Rail Mounted Gantry]")) {
						new AddRailMountedGantry().execute(null);
					} else if (selection.toString().equals("[Vessel]")) {
						new AddVessel().execute(null);
					}
				}
			}
		});

	}

	public void setFocus() {
		viewer.getControl().setFocus();
	}
}